package cn.czxy.web.servlet.zy.user;

import cn.czxy.domain.User;
import cn.czxy.service.zy.user.UserService;
import cn.czxy.utils.FileDataUtils2;
import cn.itcast.servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author zhangyuan@163.com
 * @date 2019/4/12
 * @infos
 */
public class UserServlet extends BaseServlet {
    //这是 创建filedatautils2 要传入的参数
    private static String rootPath = null;

    static {
        String t = UserService.class.getClassLoader().getResource("/").getPath();
        File file = new File( t);
        rootPath = file.getParentFile().getParentFile().getAbsolutePath();
        rootPath = rootPath+"\\"+"files\\";
    }
//

    private UserService userService = new UserService();

    /**
     * 注册方法
     * @return
     */
    public String register()
    {
        User user = toBean(User.class);

        //        zy
        if (user.getUserName()==null&&user.getUserName().length()==0&&user.getPassWord()==null&&user.getPassWord().length()==0){
            return "redirect:/user/login_user.jsp";
        }
//
        boolean falg = userService.addUser(user);

        if (!falg)
        {
            getRequest().setAttribute("msg","该用户已经被注册,请重新注册");
            return "redirect:/user/login_user.jsp";
        }

        return "redirect:/user/login_user.jsp";
    }

    /**
     * 退出方法
     * @return
     */
    public String outUser()
    {
        getRequest().getSession().removeAttribute("user");
        return "redirect:/user/login_user.jsp";
    }

    /**
     * 登录方法
     * @return
     */
    public String login()
    {

        User user = toBean(User.class);

        boolean flag = userService.login(user);

        if (!flag)
        {
            getRequest().setAttribute("msg","登录失败,用户名或者密码错误");
            return "redirect:/user/login_user.jsp";
        }

        getRequest().getSession().setAttribute("user",user);
        //lxh
        return "forward:/person?method=searchByUser";
    }


    /**
     * 添加文件方法
     * @return
     */
    public String addFile()
    {
        HttpServletRequest request = getRequest();
        HttpServletResponse response = getResponse();
        User user = (User) request.getSession().getAttribute("user");
        if (user==null||user.getUserName()==null||user.getUserName().length()==0)
        {
            return "redirect:/demand/fool.jsp";
        }
        //!!!这里是的 路径是绝对路径
        FileDataUtils2 utils2 = new FileDataUtils2(rootPath);
        utils2.addFile(user.getUserName(),request,response);
        return "redirect:/demand/fool.jsp";
    }


    /**
     * 删除文件方法
     * @return
     */
    public String delete()
    {
        String[] arr = getRequest().getParameterValues("filePath");

        if(arr==null||arr.length==0)
        {
            return "forward:/file?method=findFlieByPath&path=/";
        }
        for (int i = 0; i < arr.length; i++) {
            File file = new File(arr[i]);
            file.delete();
        }
        return "redirect:/demand/fool.jsp";
    }


    /**
     * 删除单个文件方法
     * @return
     */
    public String deleteOne()
    {
        String s = getRequest().getParameter("filePath");

        //注意返回的路径
        String address = null;
        if(s!=null)
        {
            s = getServletContext().getRealPath(s);
            File file = new File(s);
            boolean delete = file.delete();
            if (delete){
                address = "文件删除成功";
            }else{
                address = "正在缓冲请稍后重试";
            }
        }else{
            address = "视频正在缓冲请稍后重试";
        }
        getRequest().setAttribute("address",address);
        return "forward:/file?method=findFlieByPath&path=/";
    }



}
